//
//  ViewControllerThree.m
//  HBDemo
//
//  Created by RYX on 2019/3/25.
//  Copyright © 2019 tuobu. All rights reserved.
//

#import "ViewControllerThree.h"
#import "YHBTopView.h"
#import "YHBZDCell.h"
#import "YHBZDStore.h"
#import "YHBZDSNModel.h"
#import "TZFXViewController.h"

@interface ViewControllerThree ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger page;
}
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) YHBTopView *topView;
@property (nonatomic,strong) NSMutableArray *listArr;

@end

@implementation ViewControllerThree

- (YHBTopView *)topView{
    
    if (!_topView) {
        
        _topView = [[YHBTopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 25)];
    }
    
    return _topView;
}
- (NSMutableArray *)listArr{
    if (!_listArr) {
        _listArr = [[NSMutableArray alloc]init];
    }
    return _listArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
}
- (void)initCSS{
    page = 1;
    [self setNavBarWithTitle:self.realName];
    self. tableView = [[UITableView alloc] init];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorInset = UIEdgeInsetsZero;
    _tableView.separatorColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];
    [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    [_tableView registerClass:[YHBZDCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    MJRefreshNormalHeader *tabheader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self->page = 1;
        [self loadData];
    }];
    tabheader.lastUpdatedTimeLabel.hidden = YES;
    _tableView.mj_header = tabheader;
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self->page ++;
        [self loadData];
    }];
    _tableView.mj_footer = footer;
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 20;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YHBZDSNModel *model = self.listArr[indexPath.row];
    model.index = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
    YHBZDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

//    YHBZDSNModel *model = self.listArr[indexPath.row];
//    
//    if ([model.activeStatus intValue] >= 5) {
//        
//        return;
//    }
//    
//    TZFXViewController *VC = [[TZFXViewController alloc] init];
//    VC.sn = model.posSn;
//    [self.navigationController pushViewController:VC animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return self.topView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 25;
}

- (void)loadData{
    
    YHBZDStore *store = [[YHBZDStore alloc] init];
    MJWeakSelf
    [store getYHBZDSNListWithCustomerId:[UserDefaultsManager sharedInstance].user_id current:[NSString stringWithFormat:@"%ld",(long)page] size:@"100" targetId:self.targetId Success:^(NSArray * _Nonnull listArr) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        if (self->page == 1) {
            
            weakSelf.listArr = [NSMutableArray arrayWithArray:listArr];
        }else{
            
            [weakSelf.listArr addObjectsFromArray:listArr];
        }
        
        if (listArr.count < 100) {
            
            weakSelf.tableView.mj_footer.hidden = YES;
        }else{
            
            weakSelf.tableView.mj_footer.hidden = NO;
        }
        
        [weakSelf.tableView reloadData];
        
    } Failure:^(NSError * _Nonnull error) {
       
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        NSLog(@"请求出错");
    }];
}
@end
